﻿using FolderTag.DB;
using FolderTag.Model;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace FolderTag.FolderView
{
    /// <summary>
    /// TagManage.xaml 的交互逻辑
    /// </summary>
    public partial class TagManage : UserControl
    {

        MainWindow _MainWindow;

        public TagManage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗口加载完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _MainWindow = this.TryFindParent<MainWindow>();
            LoadTags();
        }

        private void Button_AddTag_Click(object sender, RoutedEventArgs e)
        {
            TagItem inputData = Tag_Flyout.DataContext as TagItem;
            if (inputData == null)
                _MainWindow.ShowModalMessageExternal("添加失败", "程序数据错误，请重试", MessageDialogStyle.Affirmative, _MainWindow.MetroDialogSettings);
            else
            {
                bool isSuccess = false;
                if (inputData.Id > 0)
                    isSuccess = FolderTagDb.Update(inputData);
                else
                {
                    if (FolderTagDb.GetByName(inputData.Name) != null)
                        _MainWindow.ShowModalMessageExternal("添加失败", "标签已存在请勿重复添加", MessageDialogStyle.Affirmative, _MainWindow.MetroDialogSettings);
                    isSuccess = FolderTagDb.Add(inputData);
                }
                if (!isSuccess)
                    _MainWindow.ShowModalMessageExternal("添加标签", "失败,数据库未知错误", MessageDialogStyle.Affirmative, _MainWindow.MetroDialogSettings);
                LoadTags(true);
                _MainWindow.Ctl_FolderControl.LoadPath("cur");
            }
            Tag_Flyout.IsOpen = false;
        }

        /// <summary>
        /// 新建标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_NewTag_Click(object sender, RoutedEventArgs e)
        {
            Tag_Flyout.Header = "新建标签";
            Tag_Flyout.DataContext = new TagItem();
            Tag_Flyout.IsOpen = true;
        }

        /// <summary>
        /// 编辑标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_EditTag_Click(object sender, RoutedEventArgs e)
        {
            Tag_Flyout.Header = "编辑标签";
            TagItem tagItem = ((sender as Button).Parent as Grid).DataContext as TagItem;
            if (tagItem != null)
            {
                Tag_Flyout.DataContext = TagItem.Clone(tagItem);
                Tag_Flyout.IsOpen = true;
            }
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_DelTag_Click(object sender, RoutedEventArgs e)
        {
            TagItem tagItem = ((sender as Button).Parent as Grid).DataContext as TagItem;
            if (tagItem != null)
            {
                var mesResult = _MainWindow.ShowModalMessageExternal("删除标签", $"是否删除标签[{tagItem.Name}]", MessageDialogStyle.AffirmativeAndNegative, _MainWindow.MetroDialogSettings);
                if (mesResult == MessageDialogResult.Affirmative)
                {
                    FolderTagDb.Delete(tagItem.Id);
                    LoadTags(true);
                    _MainWindow.Ctl_FolderControl.LoadPath("cur");
                }
            }
        }
        /// <summary>
        /// 关闭设置标签页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_CancelNewTag_Click(object sender, RoutedEventArgs e)
        {
            Tag_Flyout.IsOpen = false;
        }

        /// <summary>
        /// 过滤标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_LayerTag_Click(object sender, RoutedEventArgs e)
        {
            HashSet<int> tagIds = new HashSet<int>();
            foreach (var item in Tag_ListBox.SelectedItems)
                tagIds.Add((item as TagItem).Id);
            _MainWindow.Ctl_FolderControl.LoadPath("cur", tagIds);
        }

        /// <summary>
        /// 加载标签
        /// </summary>
        public void LoadTags(bool isRefresh = false)
        {
            var dataList = FolderTagDb.QueryList(isRefresh);
            Tag_ListBox.ItemsSource = dataList;
            Tag_ListBox_NoData.Visibility = dataList.Count() > 0 ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
